package cn.miao.hrm.controller;

import cn.miao.hrm.domain.Course;
import cn.miao.hrm.dto.SaveCourseDto;
import cn.miao.hrm.query.CourseQuery;
import cn.miao.hrm.service.ICourseService;
import cn.miao.hrm.vo.CrumbsVo;
import cn.miao.util.AjaxResult;
import cn.miao.util.PageList;
import com.baomidou.mybatisplus.plugins.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/course")
public class CourseController {
    @Autowired
    public ICourseService courseService;



    /**
     * 保存课程
     */
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public AjaxResult save(@Valid @RequestBody SaveCourseDto courseDto) {
        return courseService.saveCourse(courseDto);
    }

    /**
     * 课程上线
     */
    @RequestMapping(value = "/onLineCourse/{id}", method = RequestMethod.POST)
    public AjaxResult onLineCourse(@PathVariable("id") Long id) {
        return courseService.onLineCourse(id);
    }

    /**
     * 课程下线
     */
    @RequestMapping(value = "/offLineCourse/{id}", method = RequestMethod.POST)
    public AjaxResult offLineCourse(@PathVariable("id") Long id) {
        return courseService.offLineCourse(id);
    }


    /**
     * 删除对象
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    public AjaxResult delete(@PathVariable("id") Long id) {
        courseService.deleteById(id);
        return AjaxResult.me();
    }

    /**
     * 根据ID获取对象
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public AjaxResult get(@PathVariable("id") Long id) {
        return AjaxResult.me().setResultObj(courseService.selectById(id));
    }


    /**
     * 获取所有对象
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public AjaxResult list() {
        return AjaxResult.me().setResultObj(courseService.selectList(null));
    }

    /**
     * 分页查询数据
     */
    @RequestMapping(value = "/pagelist", method = RequestMethod.POST)
    @PreAuthorize("hasAnyAuthority('course:pagelist')")
    public AjaxResult json(@RequestBody CourseQuery query) {
        Page<Course> page = new Page<Course>(query.getPage(), query.getRows());
        page = courseService.selectPage(page);
        PageList<Course> pageList = new PageList<>(page.getTotal(), page.getRecords());
        return AjaxResult.me().setResultObj(pageList);
    }
}
